package com.yy.web.pm.billboard.service.Impl;

import com.yy.web.pm.billboard.domain.Dto.BillboardDto;
import com.yy.web.pm.operateExamine.domain.FileData;
import com.yy.web.pm.operateExamine.mapper.FileDataMapper;
import com.yy.web.pm.shopPeople.domain.ShopPeople;
import com.yy.web.pm.shopPeople.mapper.ShopPeopleMapper;
import com.yy.web.pm.user.domain.User;
import com.yy.web.pm.user.mapper.UserMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

import com.yy.web.pm.billboard.domain.BillboardData;
import com.yy.web.pm.billboard.mapper.BillboardDataMapper;
import com.yy.web.pm.billboard.service.BillboardDataService;

import static com.yy.web.pm.alarm.service.Impl.AlarmDateServiceImpl.extractOpenId;

@Service
public class BillboardDataServiceImpl implements BillboardDataService{

    @Resource
    private BillboardDataMapper billboardDataMapper;
    @Autowired
    private ShopPeopleMapper shopPeopleMapper;
    @Autowired
    private UserMapper userMapper;
    @Autowired
    private FileDataMapper fileDataMapper;

    @Override
    public int deleteByPrimaryKey(Integer id) {
        return billboardDataMapper.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(BillboardData record) {
        return billboardDataMapper.insert(record);
    }

    @Override
    public int insertSelective(BillboardData record) {
        return billboardDataMapper.insertSelective(record);
    }

    @Override
    public BillboardData selectByPrimaryKey(Integer id) {
        return billboardDataMapper.selectByPrimaryKey(id);
    }

    @Override
    public int updateByPrimaryKeySelective(BillboardData record) {
        return billboardDataMapper.updateByPrimaryKeySelective(record);
    }

    @Override
    public int updateByPrimaryKey(BillboardData record) {
        return billboardDataMapper.updateByPrimaryKey(record);
    }

    @Override
    public int updateBatch(List<BillboardData> list) {
        return billboardDataMapper.updateBatch(list);
    }

    @Override
    public int batchInsert(List<BillboardData> list) {
        return billboardDataMapper.batchInsert(list);
    }
/**
 * @Title:selectBillBoardList
 * @Description: 方法描述:广告牌列表
 * @param billboardDto:
 */
@Override
    public List<BillboardDto> selectBillBoardList(BillboardDto billboardDto) {
    User user = getUser();
    // TODO 没问题修改RIF，有问题，抛出问题
        return  billboardDataMapper.selectBillBoardList(billboardDto, user.getId());

    }

    @Override
    public void UpdateBillBoardExpire(BillboardData billboardData) {
        List<BillboardData> billboardData1 = billboardDataMapper.selectBillboardData(billboardData);//找出所有的即将过期的店铺的广告牌
        for (BillboardData data : billboardData1) {
            String shopId = data.getShopId();
            shopPeopleMapper.updateByPrimaryKey(new ShopPeople().setId(Integer.valueOf(shopId)));
        }
    }

    /**
     * @Title:SelectBillBoardExpire
     * @Description: 方法描述:查看快过期的广告牌
     * @param billboardDto:
     */


    @Override
    public List<BillboardDto> SelectBillBoardExpire(BillboardDto billboardDto) {
        return billboardDataMapper.selectBillboardDto(billboardDto);//找出所有的即将过期的店铺的广告牌
    }

    @Override
    public int UpdateBillBoard(BillboardData billboardDto) {
        //  第三方检测机构审核检测商家信息
        return billboardDataMapper.updateByPrimaryKeySelective(billboardDto);
    }
// 管理员下发Ridf标签
    @Override
    public int updateDate(Long id) {
        //下发rifd标签
        billboardDataMapper.updateByid(id);
        //记录归档
        User user = getUser();
        FileData fileData = new FileData()
                .setOperatePeopele(user.getUsername())
                .setOperateDecision("下发Ridf标签")
                .setOperateResult("1")
                .setOperateEventId(String.valueOf(id))
                .setOperateTime(new Date());
        return fileDataMapper.insert(fileData);
    }

    private User getUser() {
        //获取当前用户信息
        Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
        String openId = extractOpenId(authentication.getName());
        User user = userMapper.getUserByOpenId(openId);//获取当前用户信息
        return user;
    }


}
